app.controller('LoginCtrl', ['$rootScope', '$scope', '$state', '$translate', '$localStorage', '$cookieStore', '$auth', '$window', '$document', '$timeout', '$filter', '$http', 'cfpLoadingBar', 'SweetAlert', 'Restangular', 'api_login',
    function($rootScope, $scope, $state, $translate, $localStorage, $cookieStore, $auth, $window, $document, $timeout, $filter, $http, cfpLoadingBar, SweetAlert, Restangular, api_login) {
        $scope.loginForm = {
            username: "",
            // account:"",
            password: ""
        }
        $rootScope.app.isLoginFixed = true;
        $scope.validateUsername = function() {
            return $scope.loginForm.username == null || $scope.loginForm.username == "";
        }
        $scope.loginsign = false;
        $scope.validatePassword = function() {
            return $scope.loginForm.password == null || $scope.loginForm.password == "";
        }

        $scope.formValid = function() {
            return $scope.loginForm.username == null || $scope.loginForm.password == null || $scope.loginsign;
        }
        $scope.loginpassValid = function() {
            return $scope.loginForm.password == null || $scope.loginForm.password == "";
        }
        $scope.loginuserValid = function() {
            return $scope.loginForm.username == null || $scope.loginForm.username == "";
        }
        $rootScope.$on('auth:invalid', function(ev) {
            $state.go('login.signin');
        });
        $scope.loginsignvalid = false;
        $scope.handleLoginClick = function(e) {
            $scope.loginsignvalid = true;
            // console.log(' $scope.loginForm.='+JSON.stringify( $scope.loginForm));
            // if (isEmpty($auth.retrieveData('auth_headers'))) {
            if (!$scope.loginuserValid() && !$scope.loginpassValid()) {
                $auth.submitLogin($scope.loginForm).then(function(resp) {
                        if (resp.signedIn == true) {
                            $state.go('app.dashboard');
                            $rootScope.app.isLoginFixed = false;
                            $scope.loginsign = true;
                            // var myCache = $cacheFactory('myCache');
                            // myCache.put("userid", resp.id);
                        } else if (resp.signedIn == false) {
                            $scope.loginsign = false;
                            $scope.loginForm.password = "";
                            SweetAlert.swal("用户名或者密码验证失败，请重试！", "登录错误", "error");
                        }
                    })
                    .catch(function(resp) {
                        $scope.loginsign = false;
                        SweetAlert.swal("系统错误，请稍候重试！", "登录系统错误", "error");
                    });
            }

            // } else {
            //     SweetAlert.swal("已有用户登录", "登录系统错误", "error");
            // }
            // $auth.submitLogin($scope.loginForm)
            //     .then(function(resp) {
            //         // console.log(' resp='+JSON.stringify(resp));
            //         if (resp.signedIn == true) {
            //             $state.go('app.dashboard');
            //             $rootScope.app.isLoginFixed = false;
            //         } else if (resp.signedIn == false) {
            //             $scope.loginForm.password = "";
            //             SweetAlert.swal("用户名或者密码验证失败，请重试！", "登录错误", "error");
            //         }
            //     })
            //     .catch(function(resp) {
            //         SweetAlert.swal("系统错误，请稍候重试！", "登录系统错误", "error");
            //     });
        };
        $scope.handleloginEnter = function(e) {
            if (e.keyCode == 13) {
                $scope.handleLoginClick();
            }
        };

        $scope.viewType = "password";
        $scope.isPassword = true;

        $scope.toggleView = function() {
            $scope.isPassword = !$scope.isPassword;
            $scope.viewType = $scope.isPassword ? "password" : "text";
        }

        $scope.handleSignOutBtnClick = function() {
            $auth.signOut()
                .then(function(resp) {

                    // handle success response
                })
                .catch(function(resp) {
                    // handle error response
                });
        };


    }
]);